home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tcl8.0 / tests / expr.test < prev    next >
Encoding:
Text File  |  1997-08-15  |  32.6 KB  |  642 lines  |  [TEXT/ALFA]

  1. # Commands covered: expr
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands. Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1996-1997 Sun Microsystems, Inc.
  8. #
  9. # See the file "license.terms" for information on usage and redistribution
  10. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11. #
  12. # SCCS: @(#) expr.test 1.33 97/08/07 10:45:57
  13.  
  14. if {[string compare test [info procs test]] == 1} then {source defs}
  15.  
  16. if {([catch {expr T1()} msg] == 1) && ($msg == {unknown math function "T1"})} {
  17.     set gotT1 0
  18.     puts "This application hasn't been compiled with the \"T1\" and"
  19.     puts "\"T2\" math functions, so I'll skip some of the expr tests."
  20. } else {
  21.     set gotT1 1
  22. }
  23.  
  24. # procedures used below
  25.  
  26. proc put_hello_char {c} {
  27.     global a
  28.     append a [format %c $c]
  29.     return $c
  30. }
  31. proc hello_world {} {
  32.     global a
  33.     set a ""
  34.     set L1 [set l0 [set h_1 [set q 0]]]
  35.     for {put_hello_char [expr [put_hello_char [expr [set h 7]*10+2]]+29]} {$l0?[put_hello_char $l0]
  36.         :!$h_1} {put_hello_char $ll;expr {$L1==2?[set ll [expr 32+0-0+[set bar 0]]]:0}} {expr {[incr L1]==[expr 1+([string length "abc"]-[string length "abc"])]
  37.         ?[set ll [set l0 [expr 54<<1]]]:$ll==108&&$L1<3?
  38.         [incr ll [expr 1|1<<1]; set ll $ll; set ll $ll; set ll $ll; set ll $ll; set l0 [expr ([string length "abc"]-[string length "abc"])+([string length "abc"]-[string length "abc"])-([string length "abc"]-[string length "abc"])+([string length "abc"]-[string length "abc"])]; set l0; set l0 $l0; set l0; set l0]:$L1==4&&$ll==32?[set ll [expr 19+$h1+([string length "abc"]-[string length "abc"])-([string length "abc"]-[string length "abc"])+([string length "abc"]-[string length "abc"])-([string length "abc"]-[string length "abc"])+[set foo [expr ([string length "abc"]-[string length "abc"])+([string length "abc"]-[string length "abc"])+([string length "abc"]-[string length "abc"])]]]]
  39.         :[set q [expr $q-$h1+([string length "abc"]-[string length "abc"])-([string length "abc"]-[string length "abc"])]]};expr {$L1==5?[incr ll -8; set ll $ll; set ll]:$q&&$h1&&1};expr {$L1==4+2
  40.         ?[incr ll 3]:[expr ([string length "abc"]-[string length "abc"])+1]};expr {$ll==($h<<4)+2+0&&$L1!=6?[incr ll -6]:[set h1 [expr 100+([string length "abc"]-[string length "abc"])-([string length "abc"]-[string length "abc"])]]}
  41.         expr {$L1!=1<<3?[incr q [expr ([string length "abc"]-[string length "abc"])-1]]:[set h_1 [set ll $h1]]}
  42.     }
  43.     set a
  44. }
  45.  
  46. proc 12days {a b c} {
  47.     global xxx
  48.     expr {1<$a?[expr {$a<3?[12days -79 -13 [string range $c [12days -87 \
  49.     [expr 1-$b] [string range $c [12days -86 0 [string range $c 1 end]] \
  50.     end]] end]]:1};expr {$a<$b?[12days [expr $a+1] $b $c]:3};expr {[12days \
  51.     -94 [expr $a-27] $c]&&$a==2?$b<13?[12days 2 [expr $b+1] "%s %d %d\n"]:9
  52.     :16}]:$a<0?$a<-72?[12days $b $a "@n'+,#'/*\{\}w+/w#cdnr/+,\{\}r/*de\}+,/*\{*+,/w\{%+,/w#q#n+,/#\{l+,/n\{n+,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,\}\{w+K w'K:'+\}e#';dq#'l q#'+d'K#!/+k#;q#'r\}eKK#\}w'r\}eKK\{nl\]'/#;#q#n')\{)#\}w')\{)\{nl\]'/+#n';d\}rw' i;# )\{nl\]!/n\{n#'; r\{#w'r nc\{nl\]'/#\{l,+'K \{rw' iK\{;\[\{nl\]'/w#q#n'wk nw' iwk\{KK\{nl\]!/w\{%'l##w#' i; :\{nl\]'/*\{q#'ld;r'\}\{nlwb!/*de\}'c ;;\{nl'-\{\}rw\]'/+,\}##'*\}#nc,',#nw\]'/+kd'+e\}+;#'rdq#w! nr'/ ') \}+\}\{rl#'\{n' ')# \}'+\}##(!!/"]
  53.     :$a<-50?[string compare [format %c $b] [string index $c 0]]==0?[append \
  54.     xxx [string index $c 31];scan [string index $c 31] %c x;set x]
  55.     :[12days -65 $b [string range $c 1 end]]:[12days [expr ([string compare \
  56.     [string index $c 0] "/"]==0)+$a] $b [string range $c 1 end]]:0<$a
  57.     ?[12days 2 2 "%s"]:[string compare [string index $c 0] "/"]==0||
  58.     [12days 0 [12days -61 [scan [string index $c 0] %c x; set x] \
  59.     "!ek;dc i@bK'(q)-\[w\]*%n+r3#l,\{\}:\nuwloca-O;m .vpbks,fxntdCeghiry"] \
  60.     [string range $c 1 end]]}
  61. }
  62. proc do_twelve_days {} {
  63.     global xxx
  64.     set xxx ""
  65.     12days 1 1 1
  66.     string length $xxx
  67. }
  68.  
  69. # start of tests
  70.  
  71. catch {unset a b i x}
  72.  
  73. test expr-1.1 {TclCompileExprCmd: no expression} {
  74.     list [catch {expr  } msg] $msg
  75. } {1 {wrong # args: should be "expr arg ?arg ...?"}}
  76. test expr-1.2 {TclCompileExprCmd: one expression word} {
  77.     expr -25
  78. } -25
  79. test expr-1.3 {TclCompileExprCmd: two expression words} {
  80.     expr -8.2   -6
  81. } -14.2
  82. test expr-1.4 {TclCompileExprCmd: five expression words} {
  83.     expr 20 - 5 +10 -7
  84. } 18
  85. test expr-1.5 {TclCompileExprCmd: quoted expression word} {
  86.     expr "0005"
  87. } 5
  88. test expr-1.6 {TclCompileExprCmd: quoted expression word} {
  89.     catch {expr "0005"zxy} msg
  90.     set msg
  91. } {quoted string doesn't terminate properly}
  92. test expr-1.7 {TclCompileExprCmd: expression word in braces} {
  93.     expr {-0005}
  94. } -5
  95. test expr-1.8 {TclCompileExprCmd: expression word in braces} {
  96.     expr {{-0x1234}}
  97. } -4660
  98. test expr-1.9 {TclCompileExprCmd: expression word in braces} {
  99.     catch {expr {-0005}foo} msg
  100.     set msg
  101. } {argument word in braces doesn't terminate properly}
  102. test expr-1.10 {TclCompileExprCmd: other expression word in braces} {
  103.     expr 4*[llength "6 2"]
  104. } 8
  105. test expr-1.11 {TclCompileExprCmd: expression word terminated by ;} {
  106.     expr 4*[llength "6 2"];
  107. } 8
  108. test expr-1.12 {TclCompileExprCmd: inlined expr (in "catch") inside other catch} {
  109.     set a xxx
  110.     catch {
  111.     # Might not be a number
  112.     set a [expr 10*$a]
  113.     }
  114. } 1
  115. test expr-1.13 {TclCompileExprCmd: second level of substitutions in expr not in braces with single var reference} {
  116.     set a xxx
  117.     set x 27;  set bool {$x};  if $bool {set a foo}
  118.     set a
  119. } foo
  120. test expr-1.14 {TclCompileExprCmd: second level of substitutions in expr with comparison as top-level operator} {
  121.     set a xxx
  122.     set x 2;  set b {$x};  set a [expr $b == 2]
  123.     set a
  124. } 1
  125.  
  126. test expr-2.1 {TclCompileExpr: are builtin functions registered?} {
  127.     expr double(5*[llength "6 2"])
  128. } 10.0
  129. test expr-2.2 {TclCompileExpr: error in expr} {
  130.     catch {expr 2**3} msg
  131.     set msg
  132. } {syntax error in expression "2**3"}
  133. test expr-2.3 {TclCompileExpr: junk after legal expr} {
  134.     catch {expr 7*[llength "a b"]foo} msg
  135.     set msg
  136. } {syntax error in expression "7*2foo"}
  137. test expr-2.4 {TclCompileExpr: numeric expr string rep == formatted int rep} {
  138.     expr {0001}
  139. } 1
  140.  
  141. test expr-3.1 {CompileCondExpr: just lor expr} {expr 3||0} 1
  142. test expr-3.2 {CompileCondExpr: error in lor expr} {
  143.     catch {expr x||3} msg
  144.     set msg
  145. } {syntax error in expression "x||3"} 
  146. test expr-3.3 {CompileCondExpr: test true arm} {expr 3>2?44:66} 44
  147. test expr-3.4 {CompileCondExpr: error compiling true arm} {
  148.     catch {expr 3>2?2**3:66} msg
  149.     set msg
  150. } {syntax error in expression "3>2?2**3:66"}
  151. test expr-3.5 {CompileCondExpr: test false arm} {expr 2>3?44:66} 66
  152. test expr-3.6 {CompileCondExpr: error compiling false arm} {
  153.     catch {expr 2>3?44:2**3} msg
  154.     set msg
  155. } {syntax error in expression "2>3?44:2**3"}
  156. test expr-3.7 {CompileCondExpr: long arms & nested cond exprs} {unixOnly nonPortable} {
  157.     puts "Note: doing test expr-3.7 which can take several minutes to run"
  158.     hello_world
  159. } {Hello world}
  160. catch {unset xxx}
  161. test expr-3.8 {CompileCondExpr: long arms & nested cond exprs} {unixOnly nonPortable} {
  162.     puts "Note: doing test expr-3.8 which can take several minutes to run"
  163.     do_twelve_days
  164. } 2358
  165. catch {unset xxx}
  166.  
  167. test expr-4.1 {CompileLorExpr: just land expr} {expr 1.3&&3.3} 1
  168. test expr-4.2 {CompileLorExpr: error in land expr} {
  169.     catch {expr x&&3} msg
  170.     set msg
  171. } {syntax error in expression "x&&3"} 
  172. test expr-4.3 {CompileLorExpr: simple lor exprs} {expr 0||1.0} 1
  173. test expr-4.4 {CompileLorExpr: simple lor exprs} {expr 3.0||0.0} 1
  174. test expr-4.5 {CompileLorExpr: simple lor exprs} {expr 0||0||1} 1
  175. test expr-4.6 {CompileLorExpr: error compiling lor arm} {
  176.     catch {expr 2**3||4.0} msg
  177.     set msg
  178. } {syntax error in expression "2**3||4.0"}
  179. test expr-4.7 {CompileLorExpr: error compiling lor arm} {
  180.     catch {expr 1.3||2**3} msg
  181.     set msg
  182. } {syntax error in expression "1.3||2**3"}
  183. test expr-4.8 {CompileLorExpr: error compiling lor arms} {
  184.     list [catch {expr {"a"||"b"}} msg] $msg
  185. } {1 {expected boolean value but got "a"}}
  186. test expr-4.9 {CompileLorExpr: long lor arm} {
  187.     set a "abcdefghijkl"
  188.     set i 7
  189.     expr {[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]] || [string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]] || [string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]] || [string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]}
  190. } 1
  191.  
  192. test expr-5.1 {CompileLandExpr: just bitor expr} {expr 7|0x13} 23
  193. test expr-5.2 {CompileLandExpr: error in bitor expr} {
  194.     catch {expr x|3} msg
  195.     set msg
  196. } {syntax error in expression "x|3"} 
  197. test expr-5.3 {CompileLandExpr: simple land exprs} {expr 0&&1.0} 0
  198. test expr-5.4 {CompileLandExpr: simple land exprs} {expr 0&&0} 0
  199. test expr-5.5 {CompileLandExpr: simple land exprs} {expr 3.0&&1.2} 1
  200. test expr-5.6 {CompileLandExpr: simple land exprs} {expr 1&&1&&2} 1
  201. test expr-5.7 {CompileLandExpr: error compiling land arm} {
  202.     catch {expr 2**3&&4.0} msg
  203.     set msg
  204. } {syntax error in expression "2**3&&4.0"}
  205. test expr-5.8 {CompileLandExpr: error compiling land arm} {
  206.     catch {expr 1.3&&2**3} msg
  207.     set msg
  208. } {syntax error in expression "1.3&&2**3"}
  209. test expr-5.9 {CompileLandExpr: error compiling land arm} {
  210.     list [catch {expr {"a"&&"b"}} msg] $msg
  211. } {1 {expected boolean value but got "a"}}
  212. test expr-5.10 {CompileLandExpr: long land arms} {
  213.     set a "abcdefghijkl"
  214.     set i 7
  215.     expr {[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]] && [string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]] && [string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]] && [string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]}
  216. } 1
  217.  
  218. test expr-6.1 {CompileBitXorExpr: just bitand expr} {expr 7&0x13} 3
  219. test expr-6.2 {CompileBitXorExpr: error in bitand expr} {
  220.     catch {expr x|3} msg
  221.     set msg
  222. } {syntax error in expression "x|3"} 
  223. test expr-6.3 {CompileBitXorExpr: simple bitxor exprs} {expr 7^0x13} 20
  224. test expr-6.4 {CompileBitXorExpr: simple bitxor exprs} {expr 3^0x10} 19
  225. test expr-6.5 {CompileBitXorExpr: simple bitxor exprs} {expr 0^7} 7
  226. test expr-6.6 {CompileBitXorExpr: simple bitxor exprs} {expr -1^7} -8
  227. test expr-6.7 {CompileBitXorExpr: error compiling bitxor arm} {
  228.     catch {expr 2**3|6} msg
  229.     set msg
  230. } {syntax error in expression "2**3|6"}
  231. test expr-6.8 {CompileBitXorExpr: error compiling bitxor arm} {
  232.     catch {expr 2^x} msg
  233.     set msg
  234. } {syntax error in expression "2^x"}
  235. test expr-6.9 {CompileBitXorExpr: runtime error in bitxor arm} {
  236.     list [catch {expr {24.0^3}} msg] $msg
  237. } {1 {can't use floating-point value as operand of "^"}}
  238. test expr-6.10 {CompileBitXorExpr: runtime error in bitxor arm} {
  239.     list [catch {expr {"a"^"b"}} msg] $msg
  240. } {1 {can't use non-numeric string as operand of "^"}}
  241.  
  242. test expr-7.1 {CompileBitAndExpr: just equality expr} {expr 3==2} 0
  243. test expr-7.2 {CompileBitAndExpr: just equality expr} {expr 2.0==2} 1
  244. test expr-7.3 {CompileBitAndExpr: just equality expr} {expr 3.2!=2.2} 1
  245. test expr-7.4 {CompileBitAndExpr: just equality expr} {expr {"abc" == "abd"}} 0
  246. test expr-7.5 {CompileBitAndExpr: error in equality expr} {
  247.     catch {expr x==3} msg
  248.     set msg
  249. } {syntax error in expression "x==3"} 
  250. test expr-7.6 {CompileBitAndExpr: simple bitand exprs} {expr 7&0x13} 3
  251. test expr-7.7 {CompileBitAndExpr: simple bitand exprs} {expr 0xf2&0x53} 82
  252. test expr-7.8 {CompileBitAndExpr: simple bitand exprs} {expr 3&6} 2
  253. test expr-7.9 {CompileBitAndExpr: simple bitand exprs} {expr -1&-7} -7
  254. test expr-7.10 {CompileBitAndExpr: error compiling bitand arm} {
  255.     catch {expr 2**3&6} msg
  256.     set msg
  257. } {syntax error in expression "2**3&6"}
  258. test expr-7.11 {CompileBitAndExpr: error compiling bitand arm} {
  259.     catch {expr 2&x} msg
  260.     set msg
  261. } {syntax error in expression "2&x"}
  262. test expr-7.12 {CompileBitAndExpr: runtime error in bitand arm} {
  263.     list [catch {expr {24.0&3}} msg] $msg
  264. } {1 {can't use floating-point value as operand of "&"}}
  265. test expr-7.13 {CompileBitAndExpr: runtime error in bitand arm} {
  266.     list [catch {expr {"a"&"b"}} msg] $msg
  267. } {1 {can't use non-numeric string as operand of "&"}}
  268.  
  269. test expr-8.1 {CompileEqualityExpr: just relational expr} {expr 3>=2} 1
  270. test expr-8.2 {CompileEqualityExpr: just relational expr} {expr 2<=2.1} 1
  271. test expr-8.3 {CompileEqualityExpr: just relational expr} {expr 3.2>"2.2"} 1
  272. test expr-8.4 {CompileEqualityExpr: just relational expr} {expr {"0y"<"0x12"}} 0
  273. test expr-8.5 {CompileEqualityExpr: error in relational expr} {
  274.     catch {expr x>3} msg
  275.     set msg
  276. } {syntax error in expression "x>3"} 
  277. test expr-8.6 {CompileEqualityExpr: simple equality exprs} {expr 7==0x13} 0
  278. test expr-8.7 {CompileEqualityExpr: simple equality exprs} {expr -0xf2!=0x53} 1
  279. test expr-8.8 {CompileEqualityExpr: simple equality exprs} {expr {"12398712938788234-1298379" != ""}} 1
  280. test expr-8.9 {CompileEqualityExpr: simple equality exprs} {expr -1!="abc"} 1
  281. test expr-8.10 {CompileEqualityExpr: error compiling equality arm} {
  282.     catch {expr 2**3==6} msg
  283.     set msg
  284. } {syntax error in expression "2**3==6"}
  285. test expr-8.11 {CompileEqualityExpr: error compiling equality arm} {
  286.     catch {expr 2!=x} msg
  287.     set msg
  288. } {syntax error in expression "2!=x"}
  289.  
  290.  
  291. test expr-9.1 {CompileRelationalExpr: just shift expr} {expr 3<<2} 12
  292. test expr-9.2 {CompileRelationalExpr: just shift expr} {expr 0xff>>2} 63
  293. test expr-9.3 {CompileRelationalExpr: just shift expr} {expr -1>>2} -1
  294. test expr-9.4 {CompileRelationalExpr: just shift expr} {expr {1<<3}} 8
  295. test expr-9.5 {CompileRelationalExpr: shift expr producing LONG_MIN} {nonPortable} {
  296.     expr {1<<31}
  297. } -2147483648
  298. test expr-9.6 {CompileRelationalExpr: error in shift expr} {
  299.     catch {expr x>>3} msg
  300.     set msg
  301. } {syntax error in expression "x>>3"} 
  302. test expr-9.7 {CompileRelationalExpr: simple relational exprs} {expr 0xff>=+0x3} 1
  303. test expr-9.8 {CompileRelationalExpr: simple relational exprs} {expr -0xf2<0x3} 1
  304. test expr-9.9 {CompileRelationalExpr: error compiling relational arm} {
  305.     catch {expr 2**3>6} msg
  306.     set msg
  307. } {syntax error in expression "2**3>6"}
  308. test expr-9.10 {CompileRelationalExpr: error compiling relational arm} {
  309.     catch {expr 2<x} msg
  310.     set msg
  311. } {syntax error in expression "2<x"}
  312.  
  313. test expr-10.1 {CompileShiftExpr: just add expr} {expr 4+-2} 2
  314. test expr-10.2 {CompileShiftExpr: just add expr} {expr 0xff-2} 253
  315. test expr-10.3 {CompileShiftExpr: just add expr} {expr -1--2} 1
  316. test expr-10.4 {CompileShiftExpr: just add expr} {expr 1-0123} -82
  317. test expr-10.5 {CompileShiftExpr: error in add expr} {
  318.     catch {expr x+3} msg
  319.     set msg
  320. } {syntax error in expression "x+3"}
  321. test expr-10.6 {CompileShiftExpr: simple shift exprs} {expr 0xff>>0x3} 31
  322. test expr-10.7 {CompileShiftExpr: simple shift exprs} {expr -0xf2<<0x3} -1936
  323. test expr-10.8 {CompileShiftExpr: error compiling shift arm} {
  324.     catch {expr 2**3>>6} msg
  325.     set msg
  326. } {syntax error in expression "2**3>>6"}
  327. test expr-10.9 {CompileShiftExpr: error compiling shift arm} {
  328.     catch {expr 2<<x} msg
  329.     set msg
  330. } {syntax error in expression "2<<x"}
  331. test expr-10.10 {CompileShiftExpr: runtime error} {
  332.     list [catch {expr {24.0>>43}} msg] $msg
  333. } {1 {can't use floating-point value as operand of ">>"}}
  334. test expr-10.11 {CompileShiftExpr: runtime error} {
  335.     list [catch {expr {"a"<<"b"}} msg] $msg
  336. } {1 {can't use non-numeric string as operand of "<<"}}
  337.  
  338. test expr-11.1 {CompileAddExpr: just multiply expr} {expr 4*-2} -8
  339. test expr-11.2 {CompileAddExpr: just multiply expr} {expr 0xff%2} 1
  340. test expr-11.3 {CompileAddExpr: just multiply expr} {expr -1/2} -1
  341. test expr-11.4 {CompileAddExpr: just multiply expr} {expr 7891%0123} 6
  342. test expr-11.5 {CompileAddExpr: error in multiply expr} {
  343.     catch {expr x*3} msg
  344.     set msg
  345. } {syntax error in expression "x*3"}
  346. test expr-11.6 {CompileAddExpr: simple add exprs} {expr 0xff++0x3} 258
  347. test expr-11.7 {CompileAddExpr: simple add exprs} {expr -0xf2--0x3} -239
  348. test expr-11.8 {CompileAddExpr: error compiling add arm} {
  349.     catch {expr 2**3+6} msg
  350.     set msg
  351. } {syntax error in expression "2**3+6"}
  352. test expr-11.9 {CompileAddExpr: error compiling add arm} {
  353.     catch {expr 2-x} msg
  354.     set msg
  355. } {syntax error in expression "2-x"}
  356. test expr-11.10 {CompileAddExpr: runtime error} {
  357.     list [catch {expr {24.0+"xx"}} msg] $msg
  358. } {1 {can't use non-numeric string as operand of "+"}}
  359. test expr-11.11 {CompileAddExpr: runtime error} {
  360.     list [catch {expr {"a"-"b"}} msg] $msg
  361. } {1 {can't use non-numeric string as operand of "-"}}
  362. test expr-11.12 {CompileAddExpr: runtime error} {
  363.     list [catch {expr {3/0}} msg] $msg
  364. } {1 {divide by zero}}
  365. test expr-11.13 {CompileAddExpr: runtime error} {
  366.     list [catch {expr {2.3/0.0}} msg] $msg
  367. } {1 {divide by zero}}
  368.  
  369. test expr-12.1 {CompileMultiplyExpr: just unary expr} {expr ~4} -5
  370. test expr-12.2 {CompileMultiplyExpr: just unary expr} {expr --5} 5
  371. test expr-12.3 {CompileMultiplyExpr: just unary expr} {expr !27} 0
  372. test expr-12.4 {CompileMultiplyExpr: just unary expr} {expr ~0xff00ff} -16711936
  373. test expr-12.5 {CompileMultiplyExpr: error in unary expr} {
  374.     catch {expr ~x} msg
  375.     set msg
  376. } {syntax error in expression "~x"}
  377. test expr-12.6 {CompileMultiplyExpr: simple multiply exprs} {expr 0xff*0x3} 765
  378. test expr-12.7 {CompileMultiplyExpr: simple multiply exprs} {expr -0xf2%-0x3} -2
  379. test expr-12.8 {CompileMultiplyExpr: error compiling multiply arm} {
  380.     catch {expr 2*3%%6} msg
  381.     set msg
  382. } {syntax error in expression "2*3%%6"}
  383. test expr-12.9 {CompileMultiplyExpr: error compiling multiply arm} {
  384.     catch {expr 2*x} msg
  385.     set msg
  386. } {syntax error in expression "2*x"}
  387. test expr-12.10 {CompileMultiplyExpr: runtime error} {
  388.     list [catch {expr {24.0*"xx"}} msg] $msg
  389. } {1 {can't use non-numeric string as operand of "*"}}
  390. test expr-12.11 {CompileMultiplyExpr: runtime error} {
  391.     list [catch {expr {"a"/"b"}} msg] $msg
  392. } {1 {can't use non-numeric string as operand of "/"}}
  393.  
  394. test expr-13.1 {CompileUnaryExpr: unary exprs} {expr -0xff} -255
  395. test expr-13.2 {CompileUnaryExpr: unary exprs} {expr +000123} 83
  396. test expr-13.3 {CompileUnaryExpr: unary exprs} {expr +--++36} 36
  397. test expr-13.4 {CompileUnaryExpr: unary exprs} {expr !2} 0
  398. test expr-13.5 {CompileUnaryExpr: unary exprs} {expr +--+-62.0} -62.0
  399. test expr-13.6 {CompileUnaryExpr: unary exprs} {expr !0.0} 1
  400. test expr-13.7 {CompileUnaryExpr: unary exprs} {expr !0xef} 0
  401. test expr-13.8 {CompileUnaryExpr: error compiling unary expr} {
  402.     catch {expr ~x} msg
  403.     set msg
  404. } {syntax error in expression "~x"}
  405. test expr-13.9 {CompileUnaryExpr: error compiling unary expr} {
  406.     catch {expr !1.x} msg
  407.     set msg
  408. } {syntax error in expression "!1.x"}
  409. test expr-13.10 {CompileUnaryExpr: runtime error} {
  410.     list [catch {expr {~"xx"}} msg] $msg
  411. } {1 {can't use non-numeric string as operand of "~"}}
  412. test expr-13.11 {CompileUnaryExpr: runtime error} {
  413.     list [catch {expr ~4.0} msg] $msg
  414. } {1 {can't use floating-point value as operand of "~"}}
  415. test expr-13.12 {CompileUnaryExpr: just primary expr} {expr 0x123} 291
  416. test expr-13.13 {CompileUnaryExpr: just primary expr} {
  417.     set a 27
  418.     expr $a
  419. } 27
  420. test expr-13.14 {CompileUnaryExpr: just primary expr} {
  421.     expr double(27)
  422. } 27.0
  423. test expr-13.15 {CompileUnaryExpr: just primary expr} {expr "123"} 123
  424. test expr-13.16 {CompileUnaryExpr: error in primary expr} {
  425.     catch {expr [set]} msg
  426.     set msg
  427. } {wrong # args: should be "set varName ?newValue?"}
  428. test expr-14.1 {CompilePrimaryExpr: literal primary} {expr 1} 1
  429. test expr-14.2 {CompilePrimaryExpr: literal primary} {expr 123} 123
  430. test expr-14.3 {CompilePrimaryExpr: literal primary} {expr 0xff} 255
  431. test expr-14.4 {CompilePrimaryExpr: literal primary} {expr 00010} 8
  432. test expr-14.5 {CompilePrimaryExpr: literal primary} {expr 62.0} 62.0
  433. test expr-14.6 {CompilePrimaryExpr: literal primary} {
  434.     expr 3.1400000
  435. } 3.14
  436. test expr-14.7 {CompilePrimaryExpr: literal primary} {expr {{abcde}<{abcdef}}} 1
  437. test expr-14.8 {CompilePrimaryExpr: literal primary} {expr {{abc\
  438. def} < {abcdef}}} 1
  439. test expr-14.9 {CompilePrimaryExpr: literal primary} {expr {{abc\tde} > {abc\tdef}}} 0
  440. test expr-14.10 {CompilePrimaryExpr: literal primary} {expr {{123}}} 123
  441. test expr-14.11 {CompilePrimaryExpr: var reference primary} {
  442.     set i 789
  443.     list [expr {$i}] [expr $i]
  444. } {789 789}
  445. test expr-14.12 {CompilePrimaryExpr: var reference primary} {
  446.     set i {789}    ;# test expr's aggressive conversion to numeric semantics
  447.     list [expr {$i}] [expr $i]
  448. } {789 789}
  449. test expr-14.13 {CompilePrimaryExpr: var reference primary} {
  450.     catch {unset a}
  451.     set a(foo) foo
  452.     set a(bar) bar
  453.     set a(123) 123
  454.     set result ""
  455.     lappend result [expr $a(123)] [expr {$a(bar)<$a(foo)}]
  456.     catch {unset a}
  457.     set result
  458. } {123 1}
  459. test expr-14.14 {CompilePrimaryExpr: var reference primary} {
  460.     set i 123    ;# test "$var.0" floating point conversion hack
  461.     list [expr $i] [expr $i.0] [expr $i.0/12.0]
  462. } {123 123.0 10.25}
  463. test expr-14.15 {CompilePrimaryExpr: var reference primary} {
  464.     set i 123
  465.     catch {expr $i.2} msg
  466.     set msg
  467. } 123.2
  468. test expr-14.16 {CompilePrimaryExpr: error compiling var reference primary} {
  469.     catch {expr {$a(foo}} msg
  470.     set errorInfo
  471. } {missing )
  472.     (parsing index for array "a")
  473.     while compiling
  474. "expr {$a(foo}"}
  475. test expr-14.17 {CompilePrimaryExpr: string primary that looks like var ref} {
  476.     expr $
  477. } $
  478. test expr-14.18 {CompilePrimaryExpr: quoted string primary} {
  479.     expr "21"
  480. } 21
  481. test expr-14.19 {CompilePrimaryExpr: quoted string primary} {
  482.     set i 123
  483.     set x 456
  484.     expr "$i+$x"
  485. } 579
  486. test expr-14.20 {CompilePrimaryExpr: quoted string primary} {
  487.     set i 3
  488.     set x 6
  489.     expr 2+"$i.$x"
  490. } 5.6
  491. test expr-14.21 {CompilePrimaryExpr: error in quoted string primary} {
  492.     catch {expr "[set]"} msg
  493.     set msg
  494. } {wrong # args: should be "set varName ?newValue?"}
  495. test expr-14.22 {CompilePrimaryExpr: subcommand primary} {
  496.     expr {[set i 123; set i]}
  497. } 123
  498. test expr-14.23 {CompilePrimaryExpr: error in subcommand primary} {
  499.     catch {expr {[set]}} msg
  500.     set errorInfo
  501. } {wrong # args: should be "set varName ?newValue?"
  502.     while compiling
  503. "set"
  504.     while compiling
  505. "expr {[set]}"}
  506. test expr-14.24 {CompilePrimaryExpr: error in subcommand primary} {
  507.     catch {expr {[set i}} msg
  508.     set errorInfo
  509. } {missing close-bracket or close-brace
  510.     while compiling
  511. "set i"
  512.     while compiling
  513. "expr {[set i}"}
  514. test expr-14.25 {CompilePrimaryExpr: math function primary} {
  515.     format %.6g [expr exp(1.0)]
  516. } 2.71828
  517. test expr-14.26 {CompilePrimaryExpr: math function primary} {
  518.     format %.6g [expr pow(2.0+0.1,3.0+0.1)]
  519. } 9.97424
  520. test expr-14.27 {CompilePrimaryExpr: error in math function primary} {
  521.     catch {expr sinh::(2.0)} msg
  522.     set errorInfo
  523. } {syntax error in expression "sinh::(2.0)"
  524.     while executing
  525. "expr sinh::(2.0)"}
  526. test expr-14.28 {CompilePrimaryExpr: subexpression primary} {
  527.     expr 2+(3*4)
  528. } 14
  529. test expr-14.29 {CompilePrimaryExpr: error in subexpression primary} {
  530.     catch {expr 2+(3*[set])} msg
  531.     set errorInfo
  532. } {wrong # args: should be "set varName ?newValue?"
  533.     while compiling
  534. "set"
  535.     while compiling
  536. "expr 2+(3*[set])"}
  537. test expr-14.30 {CompilePrimaryExpr: missing paren in subexpression primary} {
  538.     catch {expr 2+(3*(4+5)} msg
  539.     set errorInfo
  540. } {syntax error in expression "2+(3*(4+5)"
  541.     while executing
  542. "expr 2+(3*(4+5)"}
  543. test expr-14.31 {CompilePrimaryExpr: unexpected token} {
  544.     catch {expr @} msg
  545.     set errorInfo
  546. } {syntax error in expression "@"
  547.     while executing
  548. "expr @"}
  549.  
  550. test expr-15.1 {CompileMathFuncCall: missing parenthesis} {
  551.     catch {expr sinh2.0)} msg
  552.     set errorInfo
  553. } {syntax error in expression "sinh2.0)"
  554.     while executing
  555. "expr sinh2.0)"}
  556. test expr-15.2 {CompileMathFuncCall: unknown math function} {
  557.     catch {expr whazzathuh(1)} msg
  558.     set errorInfo
  559. } {unknown math function "whazzathuh"
  560.     while executing
  561. "expr whazzathuh(1)"}
  562. test expr-15.3 {CompileMathFuncCall: too many arguments} {
  563.     catch {expr sin(1,2,3)} msg
  564.     set errorInfo
  565. } {too many arguments for math function
  566.     while executing
  567. "expr sin(1,2,3)"}
  568. test expr-15.4 {CompileMathFuncCall: ')' found before last required arg} {
  569.     catch {expr sin()} msg
  570.     set errorInfo
  571. } {syntax error in expression "sin()"
  572.     while executing
  573. "expr sin()"}
  574. test expr-15.5 {CompileMathFuncCall: too few arguments} {
  575.     catch {expr pow(1)} msg
  576.     set errorInfo
  577. } {too few arguments for math function
  578.     while executing
  579. "expr pow(1)"}
  580. test expr-15.6 {CompileMathFuncCall: missing ')'} {
  581.     catch {expr sin(1} msg
  582.     set errorInfo
  583. } {syntax error in expression "sin(1"
  584.     while executing
  585. "expr sin(1"}
  586. if $gotT1 {
  587.     test expr-15.7 {CompileMathFuncCall: call registered math function} {
  588.     expr 2*T1()
  589.     } 246
  590.     test expr-15.8 {CompileMathFuncCall: call registered math function} {
  591.     expr T2()*3
  592.     } 1035
  593.  
  594.     test expr-15.9 {CompileMathFuncCall: call registered math function} {
  595.     expr T3(21, 37)
  596.     } 37
  597.     test expr-15.10 {CompileMathFuncCall: call registered math function} {
  598.     expr T3(21.2, 37)
  599.     } 37.0
  600.     test expr-15.11 {CompileMathFuncCall: call registered math function} {
  601.     expr T3(-21.2, -17.5)
  602.     } -17.5
  603. }
  604.  
  605. # Check "expr" and computed command names.
  606.  
  607. test expr-16.1 {expr and computed command names} {
  608.     set i 0
  609.     set z expr
  610.     $z 1+2
  611. } 3
  612.  
  613. # Check correct conversion of operands to numbers: If the string looks like
  614. # an integer, convert to integer. Otherwise, if the string looks like a
  615. # double, convert to double.
  616.  
  617. test expr-17.1 {expr and conversion of operands to numbers} {
  618.     set x [lindex 11 0]
  619.     catch {expr int($x)}
  620.     expr {$x}
  621. } 11
  622.  
  623. # Check "expr" and interpreter result object resetting before appending
  624. # an error msg during evaluation of exprs not in {}s
  625.  
  626. test expr-18.1 {expr and interpreter result object resetting} {
  627.     proc p {} {
  628.         set t  10.0
  629.         set x  2.0
  630.         set dx 0.2
  631.         set f  {$dx-$x/10}
  632.         set g  {-$x/5}
  633.         set center 1.0
  634.         set x  [expr $x-$center]
  635.         set dx [expr $dx+$g]
  636.         set x  [expr $x+$f+$center]
  637.         set x  [expr $x+$f+$center]
  638.         set y  [expr round($x)]
  639.     }
  640.     p
  641. } 3
  642.